package com.zhudoo.zhilian.dao.repository;

import com.zhudoo.zhilian.dao.entity.Tip;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;

/**
 * @author zhudao
 */
@Repository
public interface TipRepository extends JpaRepository<Tip, Serializable>, JpaSpecificationExecutor<Tip> {

    /**
     * 查询用户发布的所有帖子
     *
     * @param userId
     * @param valid
     * @param pageable
     * @return
     */
    List<Tip> findByUserIdAndValid(Integer userId, boolean valid, Pageable pageable);

    /**
     * 添加浏览次数
     *
     * @param id
     * @param viewCount
     */
    @Modifying
    @Query(value = "update Tip  set viewCount=:viewCount where id=:id")
    void addViewCount(@Param("id") Integer id, @Param("viewCount") int viewCount);


    /**
     * 判断是否重复
     *
     * @param valid
     * @param status
     * @param onlyStr
     * @return
     */
    Tip findByValidAndStatusAndOnlyStr(boolean valid, Integer status, String onlyStr);
}
